<?php

/**
 * alltosun.com 商品静态方法 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 吴松键 (wusj@alltosun.com) $
 * $Date: 2014-8-8 下午12:36:18 $
 * $Id$
 */
class goods_helper
{

    /**
     * 获取ID索引的商品列表
     */
    public static function get_all_goods_list() {
        return array_to_filelds_assoc('id', _model('goods')->getList(array('status' => 1), ' ORDER BY `id` ASC'));
    }

    /**
     * 获取所有的商品sku列表
     */
    public static function get_goods_all_sku() {
        $sku_list = _model('sku')->getList(array(1 => 1));
        if ($sku_list) {
            foreach ($sku_list as $key => $val) {
                $goods_info = self::get_goods_info($val['goods_id']);
                if ($goods_info && $goods_info['is_sale']) {
                    $sku_list[$key]['market_price'] = $goods_info['market_price'];
                    $sku_list[$key]['sku_id'] = $val['id'];
                } else {
                    unset($sku_list[$key]);
                }
            }
        }
        return $sku_list;
    }

    /**
     * 获取指定门店的商品sku信息
     */
    public static function get_store_goods_sku_info($filter) {
        return _model('store_goods_sku')->read($filter);
    }

    /**
     * 获取当前商品的sku信息
     * @param unknown_type $sku_id
     * @param unknown_type $goods_id
     */
    public static function get_sku_info($sku_id, $goods_id) {
        $filter['id'] = $sku_id;
        $filter['goods_id'] = $goods_id;
        return _model('sku')->read($filter);
    }

    /**
     * 获取店铺的商品种类
     */
    public static function get_store_goods_stock_category_num($store_id) {
        $filter['store_id'] = $store_id;
        $filter['stock > '] = 0;
        return _model('store_goods_sku')->getTotal($filter);
    }

    /**
     * 获取店铺的商品种类总数(包括库存数是0的)
     */
    public static function get_all_store_goods_stock_category_num($store_id) {
        $filter['store_id'] = $store_id;
        $filter['stock >= '] = 0;
        return _model('store_goods_sku')->getTotal($filter);
    }

    public static function build_direct_buy_cart_list($sku_id, $buy_num, $is_get_all = false) {
        $sku_info = _uri('sku', $sku_id);
        $goods_info = _uri('goods', $sku_info['goods_id']);

        $gift_detail = array();
        $gift_list = _model('goods_gift_relation')->getList(array(
            'goods_id' => $sku_info['goods_id'],
            'is_multiple' => 1
        ));
        foreach ($gift_list as $k => $v) {
            $gift_goods_info = _uri('goods', $v['gift_id']);
            if (!$gift_goods_info['is_sale']) { // 当前商品以下架
                continue;
            }
            // 添加赠品
            $gift_detail[] = array(
                'goods_id' => $v['gift_id'],
                'goods_num' => $v['gift_num'],
                'sku_id' => $v['gift_sku_id'],
                'gift_num' => $v['gift_num'] * $buy_num,
                'goods_title' => $gift_goods_info['title']
            );
        }


        $cart_info = array(
            'goods_id' => $goods_info['id'],
            'goods_title' => $goods_info['title'],
            'goods_cover' => goods_helper::sku_cover($sku_id),
            'sku_id' => $sku_id,
            'goods_price' => $sku_info['price'],
            'stock' => $sku_info['stock'],
            'goods_num' => $buy_num,
            'is_promotion' => 0,
            'is_gift' => 0,
            'goods_notice' => $goods_info['goods_notice'],
            'gift_detail' => $gift_detail,
        );

        $cart_list = array();
        $cart_list[] = $cart_info;

        //@todo

        if ($is_get_all) {
            foreach ($gift_detail as $k => $v) {
                $sku_info = _uri('sku', $v['sku_id']);
                $goods_info = _uri('goods', $v['goods_id']);
                $cart_list[] = array(
                    'goods_id' => $v['goods_id'],
                    'goods_title' => $goods_info['title'],
                    'goods_cover' => goods_helper::sku_cover($sku_info['id']),
                    'sku_id' => $sku_info['id'],
                    // 'goods_price'  => $sku_info['price'],
                    'goods_price' => 0,
                    'stock' => $sku_info['stock'],
                    'goods_num' => $v['gift_num'],
                    'is_promotion' => 0,
                    'is_gift' => 1,
                    'goods_notice' => $goods_info['goods_notice'],
                    'gift_detail' => array(),
                );
            }
        }

        return $cart_list;
    }

    public static function get_sku_property($sku_id) {
        if (!$sku_id) {
            return false;
        }

        $list = array();
        $list = _model('sku_relation')->getList(array('sku_id' => $sku_id));

        $str = '';
        foreach ($list as $k => $v) {
            $tmp = _uri('property', $v['property_id'], 'title');
            $str .= $tmp . '：' . $v['tmp_value'] . '；';
        }

        return trim($str, '；');
    }

    public static function check_property($goods_id, $property_value_id) {
        if (!$goods_id || !$property_value_id) {
            return false;
        }

        $sku_ids = _model('sku_relation')->getFields('sku_id', array(
            'property_value_id' => $property_value_id,
            'goods_id' => $goods_id
        ));

        $stock_list = _model('sku')->getFields('stock', $sku_ids);

        $num = 0;

        foreach ($stock_list as $k => $v) {
            if ($v) {
                $num = $v;
            }
        }

        return $num;
    }

    /**
     * 获取sku列表明细
     *
     * @param int $goods_id
     * @param string $addon_price 统一加价(合伙人进货硬性加价)
     * @return array|bool
     * @throws AnException
     */
    public static function get_sku_list($goods_id = 0, $addon_price = '0.00') {
        if (!$goods_id) {
            return false;
        }

        $list = _model('sku')->getList(array('goods_id' => $goods_id));

        foreach ($list as $k => $v) {
            $list[$k]['sku_id'] = $v['id'];
            $list[$k]['price'] = num_keep_two_round($v['price'] + $addon_price);
            // TODO bbcoin 向上取整, 浮动加价如果含小数位, 同样向上取整
            $list[$k]['bbcoin'] = ceil($v['bbcoin']) + ceil($addon_price);
            $list[$k]['property'] = self::get_property_value_by_sku_id($v['id']);
        }

        return $list;
    }

    /**
     * 创建sku快照
     */
    public static function create_sku_snapshot($sku_id, $order_relation_id) {
        if (!$sku_id) {
            return false;
        }

        if (!$order_relation_id) {
            return false;
        }

        $sku_info = _uri('sku', array('id' => $sku_id, 'status' => 1));
        $order_relation_info = _uri('order_relation', array('id' => $order_relation_id, 'status' => 1));

        if (!$sku_info) {
            return false;
        }

        if (!$order_relation_info) {
            return false;
        }

        $goods_info = _uri('goods', array('id' => $sku_info['goods_id']));

        $sku_info['sku_reltion_list'] = _model('sku_relation')->getList($sku_info['id']);
        $info = array(
            'order_id' => $order_relation_info['order_id'],
            'order_relation_id' => $order_relation_info['id'],
            'sku_id' => $sku_info['id'],
            'sku_cover' => self::sku_cover($sku_info['id']),
            'goods_id' => $sku_info['goods_id'],
            'goods_title' => $goods_info['title'],
            'sku_name' => $sku_info['sku_name'],
            'sku_code' => $sku_info['sku_code'],
            'market_price' => $goods_info['market_price'],
            'price' => $sku_info['price'],
            'stock' => $sku_info['stock'],
            'view_order' => $sku_info['view_order'],
            'status' => 1,
            'goods_data' => json_encode($goods_info),
            'sku_content_data' => json_encode($sku_info),
        );

        $id = _model('sku_snapshot')->create($info);
        return $id;
    }

    /**
     * 获取快照信息
     */
    public static function get_snapshot_info($order_relation_id) {
        $info = array();
        if (!$order_relation_id) {
            return $info;
        }

        $snapshot_id = _uri('order_relation', $order_relation_id, 'sku_snapshot_id');

        if ($snapshot_id) {
            $info = _uri('sku_snapshot', $snapshot_id);
        }

        if (empty($info)) {
            $info = _uri('sku_snapshot', array('order_relation_id' => $order_relation_id));
        }

        if (empty($info)) {
            $goods_id = _uri('order_relation', $order_relation_id, 'goods_id');
            $goods_info = _uri('goods', $goods_id);

            $info = array(
                'goods_id' => $goods_info['id'],
                'goods_title' => $goods_info['title'],
                'sku_cover' => goods_helper::get_goods_cover($goods_info['id']),
                'market_price' => $goods_info['market_price'],
                'price' => $goods_info['price']
            );
        }

        return $info;
    }

    public static function get_sku_id_by_price($sku_id) {
        $sku_info = _uri('sku', $sku_id);
        if ($sku_info) {
            return $sku_info['price'];
        }
        return false;
    }

    /**
     *通过商品的sku价格  查询商品的id号
     * @return array() 商品id数组
     */
    public static function get_goods_by_price($start_price = 0, $end_price = 0) {
        $goods_ids = _model('goods')->getFields('id', array('`id` > ' => 0));
        $lower_goods_ids = array();
        $upper_goods_ids = array();
        if ($start_price) {
            $lower_goods_ids = _model('sku')->getFields('goods_id', array(' `price` < ' => (int)$start_price));

        }
        if ($end_price) {
            $upper_goods_ids = _model('sku')->getFields('goods_id', array(' `price` > ' => (int)$end_price));
        }

        $new_goods_ids = array_diff($goods_ids, array_unique($lower_goods_ids), array_unique($upper_goods_ids));

        return $new_goods_ids;
    }

    /**
     *通过商品的id  查询商品的价格区间
     * @param string $type 最大值或最小值 或区间
     * @return string 商品价格区间
     */
    public static function get_price_by_id($id, $type = '') {
        if (!$id) {
            return false;
        }

        $sku_prices = _model('sku')->getFields('price', array('goods_id' => (int)$id));

        if ($sku_prices) {
            if ($type == '') {
                return min($sku_prices) . '.00--' . max($sku_prices) . '.00';
            } elseif ($type == 'max') {
                return max($sku_prices);
            } elseif ($type == 'min') {
                return min($sku_prices);
            }

        }

        return;
    }

    /**
     * 通过商品的id  查询商品的抱抱币兑换价
     * @param string $type TODO 暂时无用
     * @return string 商品价格区间
     */
    public static function get_bbcoin_by_goods_id($goods_id, $type = '') {
        if (!$goods_id) {
            return false;
        }

        $sku_bbcoin_prices = _uri('sku', array('goods_id' => (int)$goods_id), 'bbcoin');

        return $sku_bbcoin_prices ? intval($sku_bbcoin_prices) : 0;
    }

    /**
     * 通过商品的id  查询商品的抱抱币兑差价
     * @param string $type TODO 暂时无用
     * @return string 商品价格区间
     */
    public static function get_diff_bbcoin_by_goods_id($goods_id, $type = '') {
        if (!$goods_id) {
            return false;
        }

        $sku_diff_bbcoin_prices = _uri('sku', array('goods_id' => (int)$goods_id), 'diff_bbcoin');

        return $sku_diff_bbcoin_prices ? intval($sku_diff_bbcoin_prices) : 0;
    }

    /**
     * 通过SKU id  查询商品的抱抱币兑差价
     * @param string $type TODO 暂时无用
     * @return string 商品价格区间
     */
    public static function get_diff_bbcoin_by_id($id, $type = '') {
        if (!$id) {
            return false;
        }

        $sku_diff_bbcoin_prices = _uri('sku', array('id' => (int)$id), 'diff_bbcoin');

        return $sku_diff_bbcoin_prices ? intval($sku_diff_bbcoin_prices) : 0;
    }

    /**
     * 通过商品的id  查询经销商商品的价格区间
     * @param string $type 最大值或最小值 或区间
     * @return string 商品价格区间
     */
    public static function get_store_price_by_id($store_id, $goods_id, $type = '') {
        if (!$store_id || !$goods_id) {
            return false;
        }

        $sku_prices = _model('store_goods_sku')->getFields('price', array(
            'store_id' => $store_id,
            'goods_id' => (int)$goods_id
        ));

        if ($sku_prices) {
            if ($type == '') {
                return min($sku_prices) . '--' . max($sku_prices);
            } elseif ($type == 'max') {
                return max($sku_prices);
            } elseif ($type == 'min') {
                return min($sku_prices);
            }

        }

        return;
    }

    /**
     *通过商品的id  查询商品的市场价价格区间
     * @return string 商品市场价价格区间
     */
    public static function get_market_price_by_id($id) {
        if (!$id) {
            return false;
        }

        $sku_market_prices = _model('sku')->getFields('market_price', array('goods_id' => (int)$id));

        if ($sku_market_prices) {
            return max($sku_market_prices);
        }

        return;
    }

    /**
     * 获取当前商品sku明细
     * @params $goods_id
     * @return array
     */
    public static function get_sku_info_by_goods_id($goods_id) {
        if (!$goods_id) {
            return false;
        }

        $sku_info = _model('sku')->read(array('goods_id' => (int)$goods_id));
        if ($sku_info) {
            return $sku_info;
        }

        return false;
    }

    /**
     * 获取当前商品sku的 商品差价
     * @params $goods_id
     * @return int 商品的总库存
     */
    public static function get_diff_price_by_goods_id($goods_id) {
        if (!$goods_id) {
            return false;
        }

        $sku_info = _model('sku')->read(array('goods_id' => (int)$goods_id));
        if ($sku_info) {
            return $sku_info['diff_price'];
        }

        return false;
    }

    /**
     * 获取当前商品sku的 商品差价
     * @params $sku_id
     * @return int 商品的总库存
     */
    public static function get_diff_price_by_id($sku_id) {
        if (!$sku_id) {
            return false;
        }

        $sku_info = _model('sku')->read(array('id' => (int)$sku_id));
        if ($sku_info) {
            return $sku_info['diff_price'];
        }

        return false;
    }

    /**
     *通过商品的id  查询商品的总库存
     * @return int 商品的总库存
     */
    public static function get_stock_by_id($id) {
        if (!$id) {
            return false;
        }

        $sku_stocks = _model('sku')->getFields('stock', array('goods_id' => (int)$id));

        if ($sku_stocks) {
            return array_sum($sku_stocks);
        }

        return;
    }

    /**
     *通过商品的id  查询商品的总库存
     * @return int 商品的总库存
     */
    public static function get_store_stock_by_id($store_id, $goods_id) {
        if (!$store_id || !$goods_id) {
            return false;
        }

        $sku_stocks = _model('store_goods_sku')->getFields('stock', array(
            'store_id' => $store_id,
            'goods_id' => (int)$goods_id
        ));

        if ($sku_stocks) {
            return array_sum($sku_stocks);
        }

        return;
    }

    /**
     * 查询所有库存大于0的商品ids
     */
    public static function get_goods_ids_by_stock() {
        $all_goods_ids = _model('goods')->getFields('id', array('status' => 1, 'is_sale' => 1));
        $goods_ids = array();
        foreach ($all_goods_ids as $k => $v) {
            if (goods_helper::get_stock_by_id($v) > 0) {
                $goods_ids[] = $v;
            }
        }

        if (empty($goods_ids)) {
            return false;
        }

        return $goods_ids;
    }

    /**
     *通过商品的id  获取商品的最新快照id
     * @return string 标题
     */
    public static function get_snapshot_id($goods_id) {
        if (!$goods_id) {
            return false;
        }

        $goods_snapshot_list = _model('goods_snapshot')->getList(array('goods_id' => $goods_id), ' ORDER BY `add_time` DESC ');
        if (empty($goods_snapshot_list)) {
            return false;
        }

        return $goods_snapshot_list[0]['id'];
    }

    /**
     *通过商品id获取有顺序的属性列表
     * @return array 商品有顺序的属性列表
     */
    public static function get_property_by_id($id) {
        if (!$id) {
            return false;
        }

        $property_ids = _model('sku_relation')->getFields('property_id', array('goods_id' => (int)$id), ' GROUP BY property_id ');
        if (!empty($property_ids)) {
            //通过icon表 确定第一个显示的属性
            $icon_relation_id = _uri('icon_relation', array('goods_id' => $id));
            foreach ($property_ids as $k => $v) {
                $property_value_ids = _model('property_value')->getFields('id', array('property_id' => $v));
                if (in_array($icon_relation_id, $property_value_ids)) {
                    $first_property_id = $v;
                    unset($property_ids[$k]);
                    array_unshift($property_ids, $first_property_id);
                    break;
                }
            }

            $property_list = _model('property')->getList($property_ids);
            foreach ($property_list as $k => $v) {
                $tmp = $property_list[0];
                if ($v['title'] == '颜色') {
                    $property_list[0] = $v;
                    $property_list[$k] = $tmp;
                }
            }
        } else {
            $property_list = array();
        }


        return $property_list;
    }

    /**
     *通过商品id获取属性icon列表
     * @return array 商品属性列表
     */
    public static function get_icon_by_id($id) {
        if (!$id) {
            return false;
        }

        $property_value_ids = _model('sku_relation')->getFields('property_value_id', array('goods_id' => (int)$id), ' GROUP BY property_value_id ');
        $icon_relation_list = _model('icon_relation')->getList(array(
            'goods_id' => (int)$id,
            'property_value_id' => $property_value_ids
        ));
        foreach ($icon_relation_list as $k => $v) {
            $file_info = _uri('attachment', $v['attachment_id']);
            $icon_list[$v['property_value_id']]['property_value_id'] = $v['property_value_id'];
            $icon_list[$v['property_value_id']]['attachment_path'] = $file_info['path'];
            $icon_list[$v['property_value_id']]['attachment_id'] = $file_info['id'];
        }

        return $icon_list;
    }

    /**
     *通过商品id获取属性value列表
     * @return array 商品属性value列表
     */
    public static function get_property_value_by_id($id) {
        if (!$id) {
            return false;
        }

        $property_value_list = _model('sku_relation')->getList(array('goods_id' => (int)$id), ' GROUP BY property_value_id ');
        foreach ($property_value_list as $k => $v) {
            $new_list[$v['property_value_id']] = $v;
        }

        return $new_list;
    }

    /**
     *通过商品id和属性id获取属性value列表
     * @return array 商品属性value列表
     */
    public static function get_property_value_by_property_id($goods_id, $property_id) {
        if (!$goods_id) {
            return false;
        }
        $filter = array(
            'goods_id' => $goods_id,
            'property_id' => $property_id
        );
        $property_value_list = _model('sku_relation')->getList($filter, ' GROUP BY property_value_id ');

        return $property_value_list;
    }

    /**
     *通过skuid 获取有顺序的商品属性value列表
     * @return array 商品属性value列表
     */
    public static function get_property_value_by_sku_id($sku_id) {
        if (!$sku_id) {
            return false;
        }

        $property_value_list = _model('sku_relation')->getList(array('sku_id' => (int)$sku_id));
        $new_list = array();
        if (!empty($property_value_list)) {
            $property_list = goods_helper::get_property_by_id($property_value_list[0]['goods_id']);
            foreach ($property_list as $k => $v) {
                foreach ($property_value_list as $key => $value) {
                    if ($value['property_id'] == $v['id']) {
                        $new_list[$value['property_id']]['title'] = _uri('property', $value['property_id'], 'title');
                        $new_list[$value['property_id']]['value_list'][] = $property_value_list[$key];
                    }
                }
            }
        }

        return $new_list;
    }

    public static function get_tmp_value_by_sku_id($sku_id) {
        if (!$sku_id) {
            return false;
        }
        $sku_relation_info = _model('sku_relation')->read(array('sku_id' => $sku_id));
        if (!$sku_relation_info) {
            return false;
        }
        if ($sku_relation_info) {
            return $sku_relation_info['tmp_value'];
        }
    }

    /**
     *通过skuid 获取sku图片
     * @return array sku图片
     */
    public static function get_sku_cover_by_sku_id($sku_id) {
        if (!$sku_id) {
            return false;
        }

        $filter = array('res_name' => 'sku', 'res_id' => $sku_id);
        $file_relation_list = _model('attachment_relation')->getList($filter);
        if (!empty($file_relation_list)) {
            $file_relation_info = $file_relation_list[0];
            $file_path = _uri('attachment', array('id' => $file_relation_info['attachment_id']), 'path');
        } else {
            $file_path = '';
        }

        return $file_path;
    }

    /**
     *通过goodsid 获取goods图片
     * @return array goods图片
     */
    public static function get_goods_image_list($id) {
        if (!$id) {
            return false;
        }

        $file_relation_ids = _model('attachment_relation')->getFields('attachment_id', array(
            'res_name' => 'goods',
            'res_id' => $id
        ));
        if (!empty($file_relation_ids)) {
            $file_list = _model('attachment')->getList($file_relation_ids);
        } else {
            $file_list = array();
        }

        return $file_list;
    }

    /**
     *通过skuId 获取属性值
     * @return string 如  颜色：红色  规格：大
     */
    public static function get_sku_property_value($sku_id, $view = 1) {
        if (!$sku_id) {
            return '';
        }

        $property_value = '';
        $list = self::get_sku_property_list($sku_id);
        if (!$list) {
            return '';
        }
        foreach ($list as $k => $v) {
            if ($view == 1) {
                $property_value .= $v['value'] . ' ';
            } else {
                $property_value .= $v['title'] . ':' . $v['value'] . ' ';
            }
        }

        return $property_value;
    }

    public static function get_sku_property_list($sku_id) {
        if (!$sku_id) {
            return array();
        }

        $property_list = array();
        $list = _model('sku_relation')->getList(array('sku_id' => $sku_id));

        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $property_title = _uri('property', $v['property_id'], 'title');
                $property_list[$property_title['id']]['title'] = $property_title;
                $property_list[$property_title['id']]['value'] = $v['tmp_value'];
            }
            krsort($property_list);
        }

        return $property_list;
    }

    /**
     *通过goodsid 获取当前商品评论总数
     * @return int
     */
    public static function get_comment_total($goods_id) {
        if (!$goods_id) {
            return 0;
        }

        $goods_snapshot_ids = _model('goods_snapshot')->getFields('id', array('goods_id' => $goods_id));

        $comment_total = 0;
        foreach ($goods_snapshot_ids as $k => $v) {
            $comment_total += _model('comment')->getTotal(array(
                'res_id' => $v,
                'res_name' => 'goods_snapshot',
                'status' => 1
            ));
        }

        return $comment_total;
    }

    /**
     *通过goodsid 获取当前商品评论的 快照ids列表
     * @return array();
     */
    public static function get_comment_res_ids($goods_id) {
        if (!$goods_id) {
            return 0;
        }

        return _model('goods_snapshot')->getFields('id', array('goods_id' => $goods_id));
    }

    /**
     * 通过属性值ids获取商品sku_id
     */
    public static function get_sku_id($property_value_ids) {
        if (empty($property_value_ids)) {
            return false;
        }

        $sku_ids = _model('sku_relation')->getFields(array('property_value_id' => $property_value_ids));
        $olny_sku_ids = array_unique($sku_ids);

        if (count($olny_sku_ids) == 1) {
            return $olny_sku_ids[0];
        }

        return false;
    }

    /**
     * 获取某系列下 一件最新推荐的商品的封面图
     */
    public static function get_goods_by_series($series_id) {
        $goods_list = _model('goods')->getList(array(
            'series_id' => $series_id,
            'recommend' => 1
        ), ' ORDER BY `add_time` DESC LIMIT 1');

        $cover = $goods_list[0]['cover'];
        $cover = _uri('file', $cover, 'path');

        return $cover;
    }

    /**
     * 通过goodsid 获取商品图片
     */
    public static function get_goods_cover($goods_id = '') {
        if (!$goods_id) {
            return '';
        }

        $cover = _uri('goods', array('id' => $goods_id), 'cover');
        $file_path = _uri('attachment', array('id' => $cover), 'path');

        if (!$file_path) {
            $sku_ids = _model('sku')->getFields('id', array('goods_id' => $goods_id));
            $file_path = goods_helper::get_sku_cover_by_sku_id($sku_ids);
        }

        if (!$file_path) {
            return '';
        }

        return $file_path;
    }

    public static function sku_cover($sku_id) {
        if (!$sku_id) {
            return '';
        }

        $file_path = goods_helper::get_sku_cover_by_sku_id($sku_id);
        if (!$file_path) {
            $goods_id = _uri('sku', array('id' => $sku_id), 'goods_id');
            $cover = _uri('goods', array('id' => $goods_id), 'cover');
            $file_path = _uri('attachment', array('id' => $cover), 'path');
        }

        if (!$file_path) {
            return '';
        }

        return $file_path;
    }

    public static function get_goods_info($goods_id) {
        if (!$goods_id) {
            return array();
        }
        $goods_info = _uri('goods', $goods_id);
        return $goods_info;
    }

    /**
     * 获取goods_relation信息
     * @param $goods_id
     * @param $realtion_goods_id
     * @return array
     */
    public static function get_goods_relation_info($goods_id, $realtion_goods_id) {
        if (!$goods_id || !$realtion_goods_id) {
            return array();
        }

        $info = _uri('goods_relation', array('goods_id' => $goods_id, 'relation_goods_id' => $realtion_goods_id));
        if ($info) {
            return $info;
        }

        return array();
    }

    /**
     * 通过商品id 获取 关联商品ids
     * @param $goods_id
     * @return array
     */
    public static function get_goods_relation_ids($goods_id) {
        if (!$goods_id) {
            return array();
        }

        $relation_ids = _model('goods_relation')->getFields('relation_goods_id', array('goods_id' => $goods_id));

        if ($relation_ids) {
            return $relation_ids;
        }

        return array();
    }

    /**
     * 通过主商品id 获取关联商品列表
     * @param $goods_id
     * @return array
     */
    public static function get_goods_relation_list($goods_id = 0) {
        /*
        if (!$goods_id) {
            return array();
        }*/

        $list = _model('goods_relation')->getList(array('id>' => 0));

        if ($list) {
            foreach ($list as $k => $v) {
                $goods_info = _uri('goods', array('id' => $v['relation_goods_id']));
                $list[$k]['title'] = $goods_info['title'];
                $list[$k]['price'] = $goods_info['price'];
                $list[$k]['cover'] = $goods_info['cover'];
                $sku_ids = self::get_sku_ids_by_goods_id($v['relation_goods_id']);
                $list[$k]['sku_ids'] = join(',', $sku_ids);
                $list[$k]['sku_num'] = count($sku_ids);
            }

            return $list;
        }

        return array();
    }

    public static function get_sku_ids_by_goods_id($goods_id, $has_stock = 0) {
        if (!$goods_id) {
            return array();
        }

        $filter['goods_id'] = $goods_id;
        if ($has_stock) {
            $filter['`stock` >'] = 0;
        }

        $sku_ids = _model('sku')->getFields('id', $filter);

        return $sku_ids;
    }

    public static function get_only_sku_id($goods_id) {
        if (!$goods_id) {
            return '';
        }

        $sku_ids = self::get_sku_ids_by_goods_id($goods_id, 1);

        if ($sku_ids) {
            return $sku_ids[0];
        }

        return '';
    }

    /**
     * 是否是加价购
     * @param $goods_id
     * @return bool
     */
    public static function is_relation($goods_id) {
        if (!$goods_id) {
            return false;
        }

        $info = _uri('goods_relation', array('goods_id' => $goods_id));

        if (!empty($info)) {
            return true;
        }

        return false;
    }


    /**
     * 获取商品的sku列表(价格是历史订单的价格),经销商进货订单详情使用
     */
    public static function get_old_store_sku_list_user_order_detail($store_id, $goods_id, $order_id, $addon_price = 0.00) {
        if (!$store_id || !$goods_id) {
            return array();
        }

        $sku_list = array();
        $order_relation_list = order_helper::get_order_relation_by_order_id($order_id, '', array('goods_id' => $goods_id));
        if ($order_relation_list) {
            foreach ($order_relation_list as $k => $v) {
                $sku_info = goods_helper::get_sku_info($v['sku_id'], $v['goods_id']);
                if ($sku_info) {
                    // 覆盖订单商品的单价
                    $sku_info['price'] = (string)$v['goods_price'];
                    $sku_info['property'] = '';
                    $sku_list[] = $sku_info;
                }
            }

        }
        return $sku_list;
    }

    /**
     * TODO 废弃
     * 获取商品的sku列表(价格是历史订单的价格),经销商进货订单详情使用,预计要废弃掉，2016-04-23,改用 get_old_store_sku_list_user_order_detail
     */
    public static function discard_get_old_store_sku_list_user_order_detail($store_id, $goods_id, $order_id, $addon_price = 0.00) {
        if (!$store_id || !$goods_id) {
            return array();
        }
        $sku_list = goods_helper::get_sku_list($goods_id, $addon_price);
        if ($sku_list) {
            foreach ($sku_list as $k => $v) {
                $order_relation_info = order_helper::get_order_relation_by_order_id($order_id, '', array('goods_id' => $goods_id));
                $sku_list[$k]['price'] = (string)$order_relation_info[0]['goods_price'];
            }
        }

        return $sku_list;
    }

    /**
     * TODO 废弃
     * 获取商品的sku列表(价格是历史订单的价格),门店订单使用
     */
    public static function get_old_store_sku_list($store_id, $goods_id, $order_id, $addon_price = 0.00) {
        if (!$store_id || !$goods_id) {
            return array();
        }
        $sku_list = goods_helper::get_store_sku_list($store_id, $goods_id, $addon_price);
        if ($sku_list) {
            foreach ($sku_list as $k => $v) {
                $order_relation_info = order_helper::get_order_relation_by_order_id($order_id, '', array('goods_id' => $goods_id));
                $sku_list[$k]['price'] = (string)$order_relation_info[0]['goods_price'];
            }
        }

        return $sku_list;
    }

    /**
     * 获取一个商品的sku列表
     * @param unknown_type $store_id 店铺ID
     * @param unknown_type $goods_id 商品ID
     * @param unknown_type $addon_price 连锁店的浮动价格
     * @return boolean|Ambigous <multitype:, boolean, unknown>
     */
    public static function get_store_sku_list($store_id, $goods_id, $addon_price = 0.00) {

        if (!$goods_id || !$store_id) {
            return false;
        }

        $store_info = _uri('store', $store_id);

        // 获取总店店铺信息
        $user_store_info = _uri('user_store', array('user_id' => $store_info['owner_id']));

        $filter = array('goods_id' => $goods_id);

        // 是否有过针对当前店铺改价过
        $filter['store_id'] = $user_store_info['id'];
        $list = _model('store_goods_sku')->getList($filter);

        if (empty($list)) {
            // 未改过价格，直接获取经销商拿货默认的价格,经销商订单直接返回false;
            $agency_user_store_info = _uri('user_store', array('user_id' => $store_info['agency_id']));
            if (!$agency_user_store_info) {
                return array();
            }

            $filter['store_id'] = $agency_user_store_info['id'];
            $list = _model('store_goods_sku')->getList($filter);
        }

        foreach ($list as $k => $v) {
            // 格式化价钱 保留2位小数

            $list[$k]['store_id'] = $store_info['id'];
            $list[$k]['price'] = num_keep_two_round($v['price'] + $addon_price);
            $list[$k]['stock'] = $v['stock'];
            $list[$k]['market_price'] = num_keep_two_round($v['market_price']);

            // 获取官方抱抱币定价 shiante 2016-08-01
            $list[$k]['bbcoin'] = (string)intval(self::get_bbcoin_by_goods_id($v['goods_id']));

            $list[$k]['property'] = self::get_property_value_by_sku_id($v['sku_id']);
        }

        return $list;
    }

    public static function get_store_goods_list($store_id) {
        $result = array();

        if (!$store_id) {
            return $result;
        }

        $filter = array('store_id' => $store_id, 'status' => 1);
        $store_goods_list = _model('store_goods')->getList($filter);

        foreach ($store_goods_list as $k => $v) {
            $goods_info = _uri('goods', $v['goods_id']);
            if ($goods_info['cover']) {
                $cover_path = _uri('attachment', $goods_info['cover'], 'path');
                $goods_info['cover'] = _image($cover_path, 'big-middle');
            }

            $result[$k] = $goods_info;

            $result[$k]['sku_list'] = self::get_store_sku_list($store_id, $v['goods_id']);
        }

        return $result;

    }

    /**
     * 增加销量
     */
    public static function add_sales($store_id, $sku_id, $goods_id, $goods_num) {
        $filter = array(
            'store_id' => $store_id,
            'sku_id' => $sku_id,
            'goods_id' => $goods_id
        );
        $affected = _model('store_goods_sku')->update($filter, ' SET `sale_count`=`sale_count`+' . $goods_num);
        if ($affected) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 减少库存
     */
    public static function subtract_stock($store_id, $sku_id, $goods_id, $goods_num) {
        $filter = array(
            'store_id' => $store_id,
            'sku_id' => $sku_id,
            'goods_id' => $goods_id
        );

        $affected = _model('store_goods_sku')->update($filter, ' SET `stock`=`stock`-' . $goods_num);
        if ($affected) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 清空库存
     * @param  [type] $info [description]
     * @return [type]       [description]
     */
    public static function clear_store_goods($info) {
        $id = _model('store_goods')->delete($info);
        return $id;
    }

    /**
     * 创建store_goods
     * @param  [type] $info [description]
     * @return [type]       [description]
     */
    public static function create_store_goods($info) {
        $id = _model('store_goods')->create($info);
        return $id;
    }

    /**
     * 创建store_goods_sku
     * @param  [type] $info [description]
     * @return [type]       [description]
     */
    public static function create_store_goods_sku($info) {
        $id = _model('store_goods_sku')->create($info);
        return $id;
    }

    /**
     * 更新store_goods_sku
     * @param  [type] $info [description]
     * @return [type]       [description]
     */
    public static function update_store_goods_sku($filter, $info) {
        $affected = _model('store_goods_sku')->update($filter, $info);
        return $affected;
    }

    /**
     * 根据渠道id获取商品id列表
     */
    public static function get_goods_by_channel_id($channel_id) {
        if ($channel_id) {
            return _model('channel_goods_sku_relation')->getFields('goods_id', array('channel_id' => $channel_id));
        }
        return array();
    }

    /**
     * 根据商品id获取分类信息
     *
     * @param $goods_id
     * @return array
     */
    public static function get_goods_category_by_goods_id($goods_id) {
        if (!$goods_id) {
            return array();
        }
        $category_list = goods_config::goods_category();
        $goods_cat_list = goods_config::get_goods_category_ids();
        return $category_list[$goods_cat_list[$goods_id]];
    }

    /**
     * 获取由分类索引的id列表
     *
     * @return array
     */
    public static function get_category_goods_tree() {
        $index = goods_config::get_goods_category_ids();
        $tree = array();
        foreach ($index as $k => $v) {
            if (!isset($tree[$v])) {
                $tree[$v] = array();
            }
            $tree[$v][] = $k;
        }
        ksort($tree);
        return $tree;
    }

    /**
     * 根据分类id(列表)获取商品id列表
     *
     * @param null $category_ids
     * @return array
     * @throws AnException
     */
    public static function get_goods_ids_by_category_ids($category_ids = null) {
        if (empty($category_ids)) {
            return array();
        }
        if (is_numeric($category_ids)) {
            $category_ids = array($category_ids);
        }

        $ret = _model('goods')->getFields('id', array('category_id' => $category_ids));
        return empty($ret) ? array() : $ret;
    }
}

?>